<template>
  <div class="editable-cell">
    <div v-if="editable" class="editable-cell-input-wrapper">
      <a-input
        :value="value"
        @change="handleChange"
        @pressEnter="check"
        style="width:82%;margin-right:4px"
      /><a-icon type="check" class="editable-cell-icon-check" @click="check" />
    </div>
    <div v-else class="editable-cell-text-wrapper" @click="edit">
      {{ value || '' }}
    </div>
  </div>
</template>

<script>
export default {
  props: {
    text: String
  },
  data() {
    return {
      value: this.text,
      editable: false
    }
  },
  mounted() {
    if (this.text === '') {
      this.editable = true
    }
  },
  methods: {
    handleChange(e) {
      const value = e.target.value
      this.value = value
    },
    check() {
      if (this.value === '') {
        this.editable = true
      } else {
        this.editable = false
        this.$emit('change', this.value)
      }
    },
    edit() {
      this.editable = true
    }
  }
}
</script>
